package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.ReportLjlmYsdhdfjlbBaDetail;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmYsdhdfjlbBaDetailVo;
import com.ruoyi.report_ljlm.mapper.ReportLjlmYsdhdfjlbBaDetailMapper;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.ReportLjlmYsdhdfjlbBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.ReportLjlmYsdhdfjlbBsInfoVo;
import com.ruoyi.report_ljlm.domain.ReportLjlmYsdhdfjlbBsInfo;
import com.ruoyi.report_ljlm.mapper.ReportLjlmYsdhdfjlbBsInfoMapper;
import com.ruoyi.report_ljlm.service.IReportLjlmYsdhdfjlbBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 压实度试验检测记录（环刀法）Service业务层处理
 *
 * @author zyb
 * @date 2024-11-08
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class ReportLjlmYsdhdfjlbBsInfoServiceImpl implements IReportLjlmYsdhdfjlbBsInfoService {

    private final ReportLjlmYsdhdfjlbBsInfoMapper baseMapper;
    private final ReportLjlmYsdhdfjlbBaDetailMapper detailMapper;

    /**
     * 查询压实度试验检测记录（环刀法）
     */
    @Override
    @FIllBasicInfo
    public ReportLjlmYsdhdfjlbBsInfoVo queryById(Long excelId){
        ReportLjlmYsdhdfjlbBsInfoVo reportLjlmYsdhdfjlbBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<ReportLjlmYsdhdfjlbBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<ReportLjlmYsdhdfjlbBaDetail>()
            .eq(ReportLjlmYsdhdfjlbBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            reportLjlmYsdhdfjlbBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return reportLjlmYsdhdfjlbBsInfoVo;
    }

    /**
     * 查询压实度试验检测记录（环刀法）列表
     */
    @Override
    public TableDataInfo<ReportLjlmYsdhdfjlbBsInfoVo> queryPageList(ReportLjlmYsdhdfjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportLjlmYsdhdfjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportLjlmYsdhdfjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询压实度试验检测记录（环刀法）列表
     */
    @Override
    public List<ReportLjlmYsdhdfjlbBsInfoVo> queryList(ReportLjlmYsdhdfjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportLjlmYsdhdfjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportLjlmYsdhdfjlbBsInfo> buildQueryWrapper(ReportLjlmYsdhdfjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportLjlmYsdhdfjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportLjlmYsdhdfjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportLjlmYsdhdfjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportLjlmYsdhdfjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportLjlmYsdhdfjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportLjlmYsdhdfjlbBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportLjlmYsdhdfjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportLjlmYsdhdfjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportLjlmYsdhdfjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportLjlmYsdhdfjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportLjlmYsdhdfjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportLjlmYsdhdfjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportLjlmYsdhdfjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportLjlmYsdhdfjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportLjlmYsdhdfjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportLjlmYsdhdfjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportLjlmYsdhdfjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportLjlmYsdhdfjlbBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportLjlmYsdhdfjlbBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportLjlmYsdhdfjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportLjlmYsdhdfjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportLjlmYsdhdfjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(bo.getJiancjg() != null, ReportLjlmYsdhdfjlbBsInfo::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getZuidgmd()), ReportLjlmYsdhdfjlbBsInfo::getZuidgmd, bo.getZuidgmd());
        return lqw;
    }

    /**
     * 新增压实度试验检测记录（环刀法）
     */
    @Override
    public Boolean insertByBo(ReportLjlmYsdhdfjlbBsInfoBo bo) {
        ReportLjlmYsdhdfjlbBsInfo add = BeanUtil.toBean(bo, ReportLjlmYsdhdfjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<ReportLjlmYsdhdfjlbBaDetail> details = new ArrayList<>(6);
            for (long i = 1; i <= 6; i++) {
                details.add(new ReportLjlmYsdhdfjlbBaDetail(add.getExcelId(), i));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改压实度试验检测记录（环刀法）
     */
    @Override
    @DealHeh
    public Boolean updateByBo(ReportLjlmYsdhdfjlbBsInfoBo bo) {
        ReportLjlmYsdhdfjlbBsInfo update = BeanUtil.toBean(bo, ReportLjlmYsdhdfjlbBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), ReportLjlmYsdhdfjlbBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportLjlmYsdhdfjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除压实度试验检测记录（环刀法）
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
